package com.flycms.module.solution.service;

import java.util.List;
import java.util.Objects;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.flycms.module.solution.dao.SolutionFunctionDao;
import com.flycms.module.solution.model.SolutionFunction;

/**
 *
 * <br/>Copyright 2020
 * <br/>All rights reserved
 * @author 沁言訫語
 * @version 2020年3月10日
 * @Date 2020年3月10日
 */

@Service
public class SolutionFunctionService {

    @Autowired
    protected SolutionFunctionDao solutionFunctionDao;
    
    @Transactional
    public int addSolutionFunction(SolutionFunction solutionFunction) throws Exception {
        return solutionFunctionDao.addSolutionFunction(solutionFunction);
    }
    
    @Transactional
    public int updateSolutionFunction(SolutionFunction solutionFunction) throws Exception {
        return solutionFunctionDao.updateSolutionFunction(solutionFunction);
    }
    
    @Transactional
    public int addSolutionFunctions(List<SolutionFunction> solutionFunctions, Integer solutionId) throws Exception {
        return solutionFunctionDao.addSolutionFunctions(solutionFunctions, solutionId);
    }
    
    @Transactional
    public int updateSolutionFunctions(List<SolutionFunction> solutionFunctions, Integer solutionId, Boolean single) throws Exception {
        if (Objects.equals(single, false)) {
            //single默认传null
            solutionFunctionDao.removeSolutionFunctionBySolutionId(solutionId, null);
            return solutionFunctionDao.addSolutionFunctions(solutionFunctions, solutionId);
        } else {
            int i = 0;
            for (SolutionFunction sf : solutionFunctions) {
//                solutionFunctionDao.removeSolutionFunctionBySolutionId(solutionId, sf.getId());
                solutionFunctionDao.updateSolutionFunction(sf);
                i++;
            }
            return i;
        }
    }
    
    @Transactional
    public int removeSolutionFunctions(Integer solutionId, Integer functionId) throws Exception {
        return solutionFunctionDao.removeSolutionFunctionBySolutionId(solutionId, functionId);
    }
    
    public List<SolutionFunction> getSolutionFunctionList(Integer solutionId, int pageIndex,
            int pageSize) throws Exception {
        return solutionFunctionDao.getSolutionFunctionList(solutionId, pageIndex, pageSize);
    }
    
    public int getSolutionFunctionCount(Integer solutionId) {
        return solutionFunctionDao.getSolutionFunctionCount(solutionId);
    }
    
}
